<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>First steps in Mono Winforms</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Winforms, Mono, C#, programming, tutorial">
<meta name="description" content="First steps in Mono Winforms">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href=".." title="Home">Contents</a>


<h1>First steps in Mono Winforms</h1>


<p>
In this part of the Mono Winforms tutorial, we introduce some basic programs in 
Winforms programing library.
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 


<h2>Simple</h2>

<p>
This is a simple Winforms application. 
</p>

<div class="codehead">simple.cs</div>
<pre class="code">
using System.Windows.Forms;
using System.Drawing;

public class Simple : Form
{
    public Simple()
    {
       Text = "Simple";
       Size = new Size(250, 200);
       CenterToScreen();
    }

    static public void Main()
    {
       Application.Run(new Simple());
    }
}
</pre>

<p>
This code example shows a small window on the screen. 
</p>

<pre class="explanation">
 using System.Windows.Forms;
 using System.Drawing;
</pre>

<p>
Here we use the <b class="keyword">using</b> directive, which allows 
us to use a type from an appropriate namespace without a fully 
qualified name. For example, we can now write Form instead of 
System.Windows.Forms.Form.
</p>

<pre class="explanation">
 public class Simple : Form
 {
 ...
 }
</pre>

<p>
In Winforms, any window or a dialog is a <b class="keyword">Form</b>. 
This control is a basic container, whose purpose is to display other 
child controls. Our class, Simple, inherits from a form. This way it
becomes a form itself.  
</p>

<pre class="explanation">
 Text = "Simple";
 Size = new Size(250, 200);
</pre>

<p>
<b class="keyword">Text</b> and <b class="keyword">Size</b> are properties 
of a form. Changing these properties, we modify our form control. The first 
line displays text "Simple" in the titlebar of the form control. 
The second line sets the size of the form to 250x200 px.
</p>

<pre class="explanation">
 CenterToScreen();
</pre>

<p>
This method centers our application on the screen. 
</p>

<pre class="explanation">
 static public void Main()
 {
    Application.Run(new Simple());
 }
</pre>

<p>
When compiled and run, the Main method is executed first. The code 
instantiates the Simple class
and runs it. 
</p>

<pre>
 $ gmcs -r:System.Windows.Forms.dll -r:System.Drawing.dll simple.cs
</pre>

<p>
Here is how we compile the source code. If we didn't make any mistakes, we 
should have <b>simple.exe</b> file in our current 
working directory. 
</p>

<br>
<img src="/img/gui/sharpwinf/simple.png" alt="Simple">
<div class="figure">Figure: Simple</div>


<h2>Icon</h2>

<p>
Mono means monkey in Spanish. If we do not provide an icon for our application, 
we have a head of a monkey by default. 
The next example shows, how to change this. 
</p>

<div class="codehead">icon.cs</div>
<pre class="code">
using System.Windows.Forms;
using System.Drawing;
using System;

public class MForm : Form
{
    public MForm()
    {
       Text = "Icon";
       Size = new Size(250, 200);

       try {
           Icon = new Icon("web.ico");
       } catch (Exception e) {
           Console.WriteLine(e.Message);
           Environment.Exit(1);
       }

       CenterToScreen();
    }

    static public void Main()
    {
       Application.Run(new MForm());
    }
}
</pre>

<p>
The code example shows an icon in the upper left corner of the form. 
A form's icon is the picture that represents the form in the taskbar as well as 
the icon that is displayed for the control box of the form.
</p>

<pre class="explanation">
 try {
     Icon = new Icon("web.ico");
 } catch (Exception e) {
     Console.WriteLine(e.Message);
     Environment.Exit(1);
 }
</pre>

<p>
It is a good practice to put all input output work between the try/catch keywords. 
The web.ico file must be available in the current working directory. This is the directory
from where we execute (./icon.exe) our application. 
</p>


<br>
<img src="/img/gui/sharpwinf/icon.png" alt="Icon">
<div class="figure">Figure: Icon</div>


<h2>Tooltips</h2>

<p>
A tooltip is a small rectangular pop-up window that displays a brief 
description of a control's purpose
when the user rests the pointer on the control.
</p>

<div class="codehead">tooltips.cs</div>
<pre class="code">
using System;
using System.Drawing;
using System.Windows.Forms;

class MForm : Form {

    public MForm() {
        Text = "Tooltips";
        Size = new Size(250, 200);

        ToolTip btnTlp = new ToolTip();

        btnTlp.SetToolTip(this, "This is a Form");

        Button button = new Button();
        btnTlp.SetToolTip(button, "This is a Button Control");
        button.Text = "Button";
        button.Location = new Point(30, 70);
        button.Parent = this;

        CenterToScreen();
    }
}

class MApplication {
    static void Main() {
        Application.Run(new MForm());
    }
}
</pre>

<p>
Our code example creates a tooltip for two controls. The <b class="keyword">Button</b> control
 and the <b class="keyword">Form</b> control. 
</p>


<pre class="explanation">
 ToolTip btnTlp = new ToolTip();
</pre>

<p>
Here we create the <b class="keyword">ToolTip</b> control. This instance 
is used to provide tooltips
for both controls. 
</p>

<pre class="explanation">
 btnTlp.SetToolTip(this, "This is a Form");
</pre>

<p>
Here we set a tooltip for a form. 
</p>

<pre class="explanation">
 btnTlp.SetToolTip(button, "This is a Button Control");
</pre>

<p>
And here for our button.
</p>


<pre class="explanation">
 Button button = new Button();
 btnTlp.SetToolTip(button, "This is a Button Control");
 button.Text = "Button";
 button.Location = new Point(30, 70);
 button.Parent = this;
</pre>

<p>
Notice the creation of the <b class="keyword">Button</b> control.
The <b class="keyword">Text</b> property is a label for the button. 
The <b class="keyword">Location</b> property places the button on the 
form at x=30, y = 70 px coordinates. Finally, the <b class="keyword">Parent</b> 
property determines the container, where the button
will reside. 
</p>


<br>
<img src="/img/gui/sharpwinf/tooltips.png" alt="Tooltips">
<div class="figure">Figure: Tooltips</div>


<h2>Button</h2>

<p>
Our last code example shows a button control in action. 
</p>

<div class="codehead">button.cs</div>
<pre class="code">
using System;
using System.Drawing;
using System.Windows.Forms;

class MForm : Form {
    public MForm() {
        Text = "Button";
        Size = new Size(250, 200);

        Button button = new Button();

        button.Location = new Point(30, 20);
        button.Text = "Quit";
        button.Click += new EventHandler(OnClick);
        button.MouseEnter += new EventHandler(OnEnter);

        Controls.Add(button);
        CenterToScreen();
    }

    void OnClick(object sender, EventArgs e) {
       Close();
    }

    void OnEnter(object sender, EventArgs e) {
       Console.WriteLine("Button Entered");
    }

}

class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}
</pre>

<p>
All GUI programming is event driven programming. In our example, we show a button control
on a form container. The button will listen to two events. The <b class="keyword">Click</b> 
and the <b class="keyword">MouseEnter</b> events. 
</p>


<pre class="explanation">
 button.Click += new EventHandler(OnClick);   
</pre>

<p>
This code line plugs an event handler to the <b class="keyword">Click</b> event.
When we click on the button, the <b class="keyword">OnClick()</b> method is called.  
</p>


<pre class="explanation">
 button.MouseEnter += new EventHandler(OnEnter);
</pre>

<p>
When we enter the button area with the mouse pointer, the
<b class="keyword">MouseEnter</b> event is triggerd. In this
case, our code calls the <b class="keyword">OnEnter()</b> method. 
</p>

<pre class="explanation">
 void OnClick(object sender, EventArgs e) {
     Close();
 }
</pre>

<p>
The method closes the application. 
</p>

<pre class="explanation">
 void OnEnter(object sender, EventArgs e) {
     Console.WriteLine("Button Entered");
 }
</pre>

<p>
When we enter the button control area with the mouse pointer,
"Button Entered" text is displayed in the terminal. 
</p>

<hr class="btm">

<p>
This part of the Mono Winforms tutorial showed some introductory
code examples to get you started with the Winforms programming library.
</p>



<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified December 28, 2008  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

